// https://leetcode.cn/problems/validate-stack-sequences/
// Created by ade on 2022/8/31.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    bool validateStackSequences(vector<int> &pushed, vector<int> &popped) {
        vector<int> st = {};
        int index = 0;
        for (int i = 0; i < pushed.size(); i++) {
            st.push_back(pushed[i]);
            while (!st.empty() && st.back() == popped[index]) {
                st.pop_back();
                index++;
            }
        }
//        while (!st.empty() && st.back() == popped[index]) {
//            st.pop_back();
//            index++;
//        }

        return st.empty();
    }
};

int main() {
    Solution so;
//    vector<int> pushed = {1, 2, 3, 4, 5};
//    vector<int> popped = {4, 5, 3, 2, 1};
    vector<int> pushed = {0, 2, 1};
    vector<int> popped = {0, 1, 2};
//    vector<int> popped = {1, 2, 0};
    cout << so.validateStackSequences(pushed, popped) << endl;
    return 0;
}